package com.test.gpa.test.suanfa;

import java.util.concurrent.atomic.AtomicInteger;

class Foo {
    AtomicInteger atomicInteger = new AtomicInteger(0);

    public Foo() {

    }

    public void first(Runnable printFirst) throws InterruptedException {

        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        atomicInteger.compareAndSet(0, 1);
        notify();
    }

    public synchronized void  second(Runnable printSecond) throws InterruptedException {
        if (atomicInteger.get() != 1) {
            wait();
        }

        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        atomicInteger.compareAndSet(1, 2);
        notify();
    }

    public synchronized void third(Runnable printThird) throws InterruptedException {
        if (atomicInteger.get() != 2) {
            wait();
        }
        notify();
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();

    }
}
